package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <dd>描述: ~ RoleController</dd>
 * <dd>创建时间：  21:19 2020/10/8</dd>
 * <dd>创建人： yuzhiguo</dd>
 * <dt>版本历史: </dt>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2020/10/8     yuzhiguo         1.0        1.0 Version
 */
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[角色-分页查询]data:{}", queryPageBean);
        //rpc查询结果
        PageResult pageResult = roleService.pageQuery(queryPageBean);
        //构造返回对象
        return new Result(true,"查询角色成功",pageResult);

    }

    /**
     * 添加
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds,Integer[] menuIds) {
        log.info("[角色-添加]data:{},permissionIds:{},menuIds:{}", role, permissionIds,menuIds);
        //RPC请求添加
        roleService.add(role, permissionIds,menuIds);
        return new Result(true, "新增角色成功");
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        log.info("[角色-根据id查询]id:{}", id);
        //rpc 调用业务处理
        Role role = roleService.findById(id);
        //封装返回值
        return new Result(true, "查询角色成功", role);

    }

    /**
     * 根据角色ID查询关联的权限ID集合
     * @param id
     * @return
     */
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id) {
        log.info("[角色-根据角色ID查询关联的权限ID集合]id:{}", id);
        //rpc 调用业务处理
        List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(id);
        //封装返回值
        return new Result(true, "查询权限成功", permissionIds);

    }

    /**
     * 根据角色ID查询关联的菜单ID集合
     * @param id
     * @return
     */
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id) {
        log.info("[角色-根据角色ID查询关联的菜单ID集合]id:{}", id);

        //rpc 调用业务处理
        List<Integer> menuIds = roleService.findMenuIdsByRoleId(id);
        //封装返回值
        return new Result(true, "查询权限成功", menuIds);

    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds,Integer[] menuIds) {
        log.info("[角色-编辑]data:{},permissionIds:{},menuIds:{}", role, permissionIds,menuIds);
        //RPC请求添加
        roleService.edit(role, permissionIds,menuIds);
        return new Result(true, "更新角色成功");
    }

    @RequestMapping("/delete")
    public Result deleteById(Integer id) {
        roleService.deleteById(id);
        return new Result(true, "删除角色成功");

    @RequestMapping("/findAll")
    public Result findRoleAll(){
        List<Role> roleList = roleService.findAll();
        return new Result(true,"查询角色成功",roleList);
    }
}
